
/**  
* @Title: AliPay.java
* @Package com.third.pay
* @Description: 
* @author likai
* @date 2017年10月14日
* @version V1.0  
*/

package com.third.utils.ali;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayDataDataserviceBillDownloadurlQueryRequest;
import com.alipay.api.request.AlipayFundTransOrderQueryRequest;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.request.AlipayMarketingCampaignCashCreateRequest;
import com.alipay.api.request.AlipayMarketingCampaignCashDetailQueryRequest;
import com.alipay.api.request.AlipayMarketingCampaignCashListQueryRequest;
import com.alipay.api.request.AlipayMarketingCampaignCashStatusModifyRequest;
import com.alipay.api.request.AlipayMarketingCampaignCashTriggerRequest;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradeCancelRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.request.MybankFinanceYulibaoAccountQueryRequest;
import com.alipay.api.request.MybankFinanceYulibaoCapitalPurchaseRequest;
import com.alipay.api.request.MybankFinanceYulibaoCapitalRansomRequest;
import com.alipay.api.request.MybankFinanceYulibaoPriceQueryRequest;
import com.alipay.api.request.MybankFinanceYulibaoTransHistoryQueryRequest;
import com.alipay.api.response.AlipayDataDataserviceBillDownloadurlQueryResponse;
import com.alipay.api.response.AlipayFundTransOrderQueryResponse;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.alipay.api.response.AlipayMarketingCampaignCashCreateResponse;
import com.alipay.api.response.AlipayMarketingCampaignCashDetailQueryResponse;
import com.alipay.api.response.AlipayMarketingCampaignCashListQueryResponse;
import com.alipay.api.response.AlipayMarketingCampaignCashStatusModifyResponse;
import com.alipay.api.response.AlipayMarketingCampaignCashTriggerResponse;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradeCancelResponse;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradePayResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.alipay.api.response.MybankFinanceYulibaoAccountQueryResponse;
import com.alipay.api.response.MybankFinanceYulibaoCapitalPurchaseResponse;
import com.alipay.api.response.MybankFinanceYulibaoCapitalRansomResponse;
import com.alipay.api.response.MybankFinanceYulibaoPriceQueryResponse;
import com.alipay.api.response.MybankFinanceYulibaoTransHistoryQueryResponse;
import com.third.base.ali.AlipayConstants;
import com.third.base.ali.AlipayParam;
import com.third.base.ali.AlipayRequest;
import com.utils.httpservlet.RequestUtil;

/**
 * @ClassName: 支付宝支付:当面付、app支付、手机网站支付、电脑网站支付、花呗分期
 * @Description: 支付宝功能：商户单笔转账到支付宝账户、发红包、余利包存钱
 * @author likai
 * @date 2017年10月14日
 * @支付宝支付文档：https://doc.open.alipay.com/
 */

public class AliPayUtil{
	public static Logger log = LoggerFactory.getLogger(AliPayUtil.class);
	private AlipayConfig alipayConfig;// 支付宝支付配置
	private String signType = "RSA2";// 商户生成签名字符串所使用的签名算法类型，目前支持RSA2和RSA，推荐使用RSA2
	private boolean useSandbox;// 是否是沙箱环境true是
	private String notifyUrl;// 通知回掉路径
	private final String format = "json";// 参数返回格式，只支持json
	private final String URL;// 支付网关
	private AlipayClient alipayClient;
	private static AliPayUtil aliPay;

	private AliPayUtil(AlipayConfig config) {
		this(config, config.getNotifyUrl());		
	}

	private AliPayUtil(AlipayConfig config, String notifyUrl) {
		super();
		this.alipayConfig = config;
		this.useSandbox = config.getUseSandbox();
		this.notifyUrl = notifyUrl;
		if (this.useSandbox) {
			URL = AlipayConstants.SNADBOX_DOMAIN;// 沙箱环境
		} else {
			URL = AlipayConstants.PRODUCT_DOMAIN;// 正式环境
		}
		alipayClient = new DefaultAlipayClient(URL, config.getAppID(), config.getAppPrivateKey(), format,
				config.getCharset(), config.getAlipayPublicKey(), signType);
	}
	public static AliPayUtil create(AlipayConfig config) {
		if (aliPay==null) {
			aliPay=new AliPayUtil(config);
		}
		return aliPay;
	}

	/*-----------------------------------------支付宝支付:当面付、app支付、手机网站支付、电脑网站支付、花呗分期---------------------------------------------*/
	
	//---------------------------------------------------当面付：条码支付、声波付、扫码付-----------------------------------------------------------/
	/**
	 * 作用：条码支付<br>
	 *
	 * 场景：条码支付是支付宝给到线下传统行业的一种收款方式。商家使用扫码枪等条码识别设备扫描用户支付宝钱包上的条码/二维码，完成收款。
	 *		 用户仅需出示付款码，所有收款操作由商家端完成。
	 * 	           
	 * @return trade_no	支付宝28位交易号
	 */
	public AlipayTradePayResponse barCodePay(AlipayParam alipayParam) throws Exception {
		AlipayTradePayRequest alipayRequest = new AlipayTradePayRequest();// 创建API对应的request
		alipayRequest.setNotifyUrl(notifyUrl);
		alipayRequest.setReturnUrl(alipayParam.getReturn_url());
		
		alipayRequest.setBizContent(AlipayRequest.barCodePay(alipayParam));
		AlipayTradePayResponse response = alipayClient.execute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}

	/**
	 * 作用：声波付<br>
	 *
	 * 场景：声波支付是指用户打开支付宝钱包内“当面付-声波付”，对准商家所提供的声波接收终端
	 *     （如：麦克风或带声波支付功能的自助售货机），并完成支付的模式。该模式适用于线下实体店支付、自助售货机等场景。
	 * 
	 * @return trade_no	支付宝28位交易号
	 */
	public AlipayTradePayResponse waveCodePay(AlipayParam alipayParam) throws Exception {
		AlipayTradePayRequest alipayRequest = new AlipayTradePayRequest();// 创建API对应的request
		alipayRequest.setNotifyUrl(notifyUrl);
		
		alipayRequest.setBizContent(AlipayRequest.waveCodePay(alipayParam));
		AlipayTradePayResponse response = alipayClient.execute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}
	
	/**
	 * 作用：扫码付 <br>
	 * 描述：收银员通过收银台或商户后台调用支付宝接口，生成二维码后，展示给用户，由用户扫描二维码完成订单支付。
	 *
	 * 场景：扫码支付，指用户打开支付宝钱包中的“扫一扫”功能，扫描商家展示在某收银场景下的二维码并进行支付的模式。
	 * 		 该模式适用于线下实体店支付、面对面支付等场景。
	 * 	            
	 * @return qr_code	订单二维码（有效时间2小时）的内容，开发者需要自己使用工具根据内容生成二维码图片
	 * 
	 */
	public AlipayTradePrecreateResponse scanCodePay(AlipayParam alipayParam) throws Exception {
		AlipayTradePrecreateRequest alipayRequest = new AlipayTradePrecreateRequest();//创建API对应的request类
		alipayRequest.setNotifyUrl(notifyUrl);
		
		alipayRequest.setBizContent(AlipayRequest.scanCodePay(alipayParam));
		AlipayTradePrecreateResponse response = alipayClient.execute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}
	
	//---------------------------------------------------app支付-----------------------------------------------------------/
	
	/**
	 * 作用：app支付 <br>
	 *
	 * 场景：适用于商家在App应用中集成支付宝支付功能。
	 * 		商家APP调用支付宝提供的SDK调用支付宝客户端内的支付模块，商家APP会跳转到支付宝中完成支付，支付完后跳回到商家APP内，
	 * 		最后展示支付结果。目前支持手机系统有：iOS（苹果）、Android（安卓）。
	 * 
	 * 费率：0.6%；特殊行业费率：1.2%
	 *             
	 * @return trade_no	支付宝28位交易号
	 * 
	 */
	public AlipayTradeAppPayResponse appPay(AlipayParam alipayParam) throws Exception {
		AlipayTradeAppPayRequest alipayRequest = new AlipayTradeAppPayRequest();//创建API对应的request类
		alipayRequest.setNotifyUrl(notifyUrl);
		
		alipayRequest.setBizContent(AlipayRequest.appPay(alipayParam));
		AlipayTradeAppPayResponse response = alipayClient.sdkExecute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}

	//---------------------------------------------------手机网站支付-----------------------------------------------------------/
	
	/**
	 * 作用：手机网站支付 <br>
	 *
	 * 场景：适用于商家在移动端网页应用中集成支付宝支付功能。
	 *		 商家在网页中调用支付宝提供的网页支付接口调起支付宝客户端内的支付模块，商家网页会跳转到支付宝中完成支付，
	 * 		 支付完后跳回到商家网页内，最后展示支付结果。若无法唤起支付宝客户端，则在一定的时间后会自动进入网页支付流程。
	 * 	     
	 * 费率：0.6%；特殊行业费率：1.2% 
	 *        
	 * @return body	html页面
	 * 
	 */
	public AlipayTradeWapPayResponse wapPay(AlipayParam alipayParam) throws Exception {
		AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request类
		alipayRequest.setReturnUrl(alipayParam.getReturn_url());
		alipayRequest.setNotifyUrl(notifyUrl);
		
		alipayRequest.setBizContent(AlipayRequest.wapPay(alipayParam));
		AlipayTradeWapPayResponse response = alipayClient.pageExecute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			log.error("支付宝创建支付订单失败返回："+response);
			return null;
		}
	}
	
	//---------------------------------------------------电脑网站支付-----------------------------------------------------------/
	

	/**
	 * 作用：电脑网站支付 <br>
	 *
	 * 场景：适用于pc端支付
	 * 	            
	 * 费率：0.6%；特殊行业费率：1.2%
	 * 
	 * @return body	html页面
	 * 
	 */
	public AlipayTradePagePayResponse pcPay(AlipayParam alipayParam) throws Exception {
		AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();//创建API对应的request类
		alipayRequest.setReturnUrl(alipayParam.getReturn_url());
		alipayRequest.setNotifyUrl(notifyUrl);
		
		alipayRequest.setBizContent(AlipayRequest.pcPay(alipayParam));
		AlipayTradePagePayResponse response = alipayClient.pageExecute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}
	
	/**
	 * 作用：统一收单线下交易查询 <br>
	 *
	 * 场景：该接口提供所有支付宝支付订单的查询，商户可以通过该接口主动查询订单状态，完成下一步的业务逻辑。
	 *       需要调用查询接口的情况： 当商户后台、网络、服务器等出现异常，商户系统最终未接收到支付通知； 调用支付接口后，
	 *       返回系统错误或未知交易状态情况； 调用alipay.trade.pay，返回INPROCESS的状态； 调用alipay.trade.cancel之前，需确认支付状态；
	 * 	            	
	 * @return 
	 * 
	 */
	public AlipayTradeQueryResponse tradeQuery(AlipayParam alipayParam) throws Exception {
		AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();//创建API对应的request类
		
		alipayRequest.setBizContent(AlipayRequest.tradeQuery(alipayParam));
		AlipayTradeQueryResponse response = alipayClient.execute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}
	
	/**
	 * 作用：统一收单交易撤销接口 <br>
	 *
	 * 场景：支付交易返回失败或支付系统超时，调用该接口撤销交易。如果此订单用户支付失败，支付宝系统会将此订单关闭；
	 * 		如果用户支付成功，支付宝系统会将此订单资金退还给用户。 注意：只有发生支付系统超时或者支付结果未知时可调用撤销，
	 * 		其他正常支付的单如需实现相同功能请调用申请退款API。提交支付交易后调用【查询订单API】，没有明确的支付结果再调用【撤销订单API】。
	 * 	            	
	 * @return 
	 * 
	 */
	public AlipayTradeCancelResponse cancel(AlipayParam alipayParam) throws Exception {
		AlipayTradeCancelRequest alipayRequest = new AlipayTradeCancelRequest();//创建API对应的request类
		
		alipayRequest.setBizContent(AlipayRequest.cancel(alipayParam));
		AlipayTradeCancelResponse response = alipayClient.execute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}
	
	/**
	 * 作用：统一收单交易退款接口 <br>
	 *
	 * 场景：当交易发生之后一段时间内，由于买家或者卖家的原因需要退款时，卖家可以通过退款接口将支付款退还给买家，
	 * 		 支付宝将在收到退款请求并且验证成功之后，按照退款规则将支付款按原路退到买家帐号上。 交易超过约定时间（签约时设置的可退款时间）
	 * 		 的订单无法进行退款 支付宝退款支持单笔交易分多次退款，多次退款需要提交原支付订单的商户订单号和设置不同的退款单号。一笔退款失败后重新提交，
	 * 		 要采用原来的退款单号。总退款金额不能超过用户实际支付金额
	 * 	            	
	 * @return 
	 * 
	 */
	public AlipayTradeRefundResponse refund(AlipayParam alipayParam) throws Exception {
		AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();//创建API对应的request类
		
		alipayRequest.setBizContent(AlipayRequest.refund(alipayParam));
		AlipayTradeRefundResponse response = alipayClient.execute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}
	
	/**
	 * 作用：查询对账单下载地址<br>
	 *
	 * 场景：为方便商户快速查账，支持商户通过本接口获取商户离线账单下载地址
	 * 	            	
	 * @return 
	 * 
	 */
	public AlipayDataDataserviceBillDownloadurlQueryResponse downloadurl(AlipayParam alipayParam) throws Exception {
		AlipayDataDataserviceBillDownloadurlQueryRequest alipayRequest = new AlipayDataDataserviceBillDownloadurlQueryRequest();//创建API对应的request类
		
		alipayRequest.setBizContent(AlipayRequest.downloadurl(alipayParam));
		AlipayDataDataserviceBillDownloadurlQueryResponse response = alipayClient.execute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}
	
	
	
	
	/*---------------------------------------------支付宝功能：商户单笔转账到支付宝账户、发红包、余利包存钱---------------------------------------------*/
	
	//---------------------------------------------------商户单笔转账到支付宝账户-----------------------------------------------------------/
	
	/**
	 * 作用：电商户单笔转账到支付宝账户 <br>
	 *
	 * 场景：单笔转账到支付宝账户是基于支付宝的资金处理能力推出的一款产品，为了满足支付宝商户向其他支付宝账户单笔转账的需求，
	 * 		针对具备开发能力的商户，提供通过API接口完成单笔转账的功能。该产品适用行业较广，可用于商户间的货款结算，商户给个人用户发放佣金等。
	 * 		目前仅支持账户余额渠道付款。
	 * 	            
	 * 费率：免费
	 * 
	 * @return 
	 * 
	 */
	public AlipayFundTransToaccountTransferResponse transfer(AlipayParam alipayParam) throws Exception {
		AlipayFundTransToaccountTransferRequest alipayRequest = new AlipayFundTransToaccountTransferRequest();//创建API对应的request类
		
		alipayRequest.setBizContent(AlipayRequest.transfer(alipayParam));
		AlipayFundTransToaccountTransferResponse response = alipayClient.execute(alipayRequest);
		return response;
	}
	
	/**
	 * 作用：转账查询 <br>
	 *
	 * 场景：商户可通过该接口查询转账订单的状态、支付时间等相关信息，主要应用于B2C转账订单查询的场景
	 * 	            	
	 * @return 
	 * 
	 */
	public AlipayFundTransOrderQueryResponse transferQuery (AlipayParam alipayParam) throws Exception {
		AlipayFundTransOrderQueryRequest alipayRequest = new AlipayFundTransOrderQueryRequest();//创建API对应的request类
		
		alipayRequest.setBizContent(AlipayRequest.transferQuery(alipayParam));
		AlipayFundTransOrderQueryResponse response = alipayClient.execute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}
	
	//---------------------------------------------------发红包-----------------------------------------------------------/
	
	//流程：1首先创建现金红包活动，设置红包总额、个数等信息；2将创建好的红包发放给指定用户
	/**
	 * 作用：创建现金活动 <br>
	 *
	 * 场景：商户可以根据自己的营销需求创建现金红包活动，设计大转盘游戏、刮刮卡、成为会员即送现金等丰富多彩的玩法。
	 * 		商户不但可以控制活动预算，还可以控制用户领取次数，领取频率等，让活动设计更贴合商户需求。用户领取现金红包后，
	 * 		资金将直接进支付宝余额，并在app首页的红包应用中，查看到出资商户的品牌信息。若红包没有被领取完，资金将会在红包失效时（有效时间由商户创建现金红包时指定）退回到商户的支付宝账户中。
	 * 	            	
	 * @return 
	 * 
	 */
	public AlipayMarketingCampaignCashCreateResponse createCash (AlipayParam alipayParam) throws Exception {
		AlipayMarketingCampaignCashCreateRequest alipayRequest = new AlipayMarketingCampaignCashCreateRequest();//创建API对应的request类
		
		alipayRequest.setBizContent(AlipayRequest.createCash(alipayParam));
		AlipayMarketingCampaignCashCreateResponse response = alipayClient.execute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}

	/**
	 * 作用：发放红包给用户 <br>
	 *
	 * 场景：创建现金活动后，将红包发给指定用户
	 * 	            	
	 * @return 
	 * 
	 */
	public AlipayMarketingCampaignCashTriggerResponse sendCash (AlipayParam alipayParam) throws Exception {
		AlipayMarketingCampaignCashTriggerRequest alipayRequest = new AlipayMarketingCampaignCashTriggerRequest();//创建API对应的request类
		
		alipayRequest.setBizContent(AlipayRequest.sendCash(alipayParam));
		AlipayMarketingCampaignCashTriggerResponse response = alipayClient.execute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}
	
	/**
	 * 作用：更改现金活动状态 <br>
	 *
	 * 场景：商户更改现金活动状态，启动/暂停
	 * 	            	
	 * @return 
	 * 
	 */
	public AlipayMarketingCampaignCashStatusModifyResponse updateCashStatus (AlipayParam alipayParam) throws Exception {
		AlipayMarketingCampaignCashStatusModifyRequest alipayRequest = new AlipayMarketingCampaignCashStatusModifyRequest();//创建API对应的request类
		
		alipayRequest.setBizContent(AlipayRequest.updateCashStatus(alipayParam));
		AlipayMarketingCampaignCashStatusModifyResponse response = alipayClient.execute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}
	
	/**
	 * 作用：现金活动列表查询<br>
	 *
	 * 场景：商户通过开放平台查询自己创建的现金活动列表
	 * 	            	
	 * @return 
	 * 
	 */
	public AlipayMarketingCampaignCashListQueryResponse cashQuery (AlipayParam alipayParam) throws Exception {
		AlipayMarketingCampaignCashListQueryRequest alipayRequest = new AlipayMarketingCampaignCashListQueryRequest();//创建API对应的request类
		
		alipayRequest.setBizContent(AlipayRequest.cashQuery(alipayParam));
		AlipayMarketingCampaignCashListQueryResponse response = alipayClient.execute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}
	
	/**
	 * 作用：现金活动详情查询<br>
	 *
	 * 场景：商户通过开放平台查询自己创建的现金活动详情
	 * 	            	
	 * @return 
	 * 
	 */
	public AlipayMarketingCampaignCashDetailQueryResponse cashDetailQuery (AlipayParam alipayParam) throws Exception {
		AlipayMarketingCampaignCashDetailQueryRequest alipayRequest = new AlipayMarketingCampaignCashDetailQueryRequest();//创建API对应的request类
		
		alipayRequest.setBizContent(AlipayRequest.cashDetailQuery(alipayParam));
		AlipayMarketingCampaignCashDetailQueryResponse response = alipayClient.execute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}
	
	
	//---------------------------------------------------余利宝-----------------------------------------------------------/
	
	/**
	 * 作用：余利宝申购<br>
	 *
	 * 场景：购买余利宝产品
	 * 	            	
	 * @return 
	 * 
	 */
	public MybankFinanceYulibaoCapitalPurchaseResponse purchase(AlipayParam alipayParam) throws Exception {
		MybankFinanceYulibaoCapitalPurchaseRequest alipayRequest = new MybankFinanceYulibaoCapitalPurchaseRequest();//创建API对应的request类
		
		alipayRequest.setBizContent(AlipayRequest.purchase(alipayParam));
		MybankFinanceYulibaoCapitalPurchaseResponse response = alipayClient.execute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}
	
	/**
	 * 作用：余利宝赎回<br>
	 *
	 * 场景：卖掉余利宝产品
	 * 	            	
	 * @return 
	 * 
	 */
	public MybankFinanceYulibaoCapitalRansomResponse ransom(AlipayParam alipayParam) throws Exception {
		MybankFinanceYulibaoCapitalRansomRequest alipayRequest = new MybankFinanceYulibaoCapitalRansomRequest();//创建API对应的request类
		
		alipayRequest.setBizContent(AlipayRequest.ransom(alipayParam));
		MybankFinanceYulibaoCapitalRansomResponse response = alipayClient.execute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}
	
	/**
	 * 作用：余利宝历史交易查询<br>
	 *
	 * 场景：余利宝历史交易查询
	 * 	            	
	 * @return 
	 * 
	 */
	public MybankFinanceYulibaoTransHistoryQueryResponse historyQuery(AlipayParam alipayParam) throws Exception {
		MybankFinanceYulibaoTransHistoryQueryRequest alipayRequest = new MybankFinanceYulibaoTransHistoryQueryRequest();//创建API对应的request类
		
		alipayRequest.setBizContent(AlipayRequest.historyQuery(alipayParam));
		MybankFinanceYulibaoTransHistoryQueryResponse response = alipayClient.execute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}
	
	/**
	 * 作用：余利宝账户和收益查询<br>
	 *
	 * 场景：余利宝账户（总份额、可用余额、冻结份额等）及收益（包括近1日、1周、历史累计收益等）查询
	 * 	            	
	 * @return 
	 * 
	 */
	public MybankFinanceYulibaoAccountQueryResponse accountQuery(AlipayParam alipayParam) throws Exception {
		MybankFinanceYulibaoAccountQueryRequest alipayRequest = new MybankFinanceYulibaoAccountQueryRequest();//创建API对应的request类
		
		alipayRequest.setBizContent(AlipayRequest.accountQuery(alipayParam));
		MybankFinanceYulibaoAccountQueryResponse response = alipayClient.execute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}
	
	/**
	 * 作用：查询余利宝行情信息<br>
	 *
	 * 场景：查询余利宝行情信息（七日年化收益率、万份收益金额）
	 * 	            	
	 * @return 
	 * 
	 */
	public MybankFinanceYulibaoPriceQueryResponse priceQuery(AlipayParam alipayParam) throws Exception {
		MybankFinanceYulibaoPriceQueryRequest alipayRequest = new MybankFinanceYulibaoPriceQueryRequest();//创建API对应的request类
		
		alipayRequest.setBizContent(AlipayRequest.priceQuery(alipayParam));
		MybankFinanceYulibaoPriceQueryResponse response = alipayClient.execute(alipayRequest);
		if (response.isSuccess()) {
			return response;
		} else {
			return null;
		}
	}
	
	//---------------------------------------------------工具类-----------------------------------------------------
	 /**
     * 处理 支付结果通知中返回数据,验证签名。
     */
    public Map<String, String> processNotify(HttpServletRequest request) throws Exception {
    	Map<String, String> notifyData=RequestUtil.getParams(request);
    	String trade_status=notifyData.get("trade_status");
    	if ("TRADE_SUCCESS".equals(trade_status) || "TRADE_FINISHED".equals(trade_status)) {
			boolean signVerified = AlipaySignature.rsaCheckV1(notifyData, alipayConfig.getAlipayPublicKey(),alipayConfig.getCharset(), "RSA2");// 调用SDK验证签名
			if (signVerified) {
				return notifyData;
			} else {
				throw new Exception(String.format("支付宝回调------------签名认证失败: %s", notifyData));
			}	
		} else {
			throw new Exception(String.format("支付宝回调------------支付宝支付失败: %s", notifyData));
		}
    }
}
